package com.neno.utils;

import com.neno.entity.Person;

import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.util.List;

/**
 * @Author: root
 * @Date: 2018/11/10 3:42
 */
public class NioFileUtils {
    private static RandomAccessFile aFile = null;
    private static FileChannel inChannel = null;
    private static final ByteBuffer buf = ByteBuffer.allocate(1024);

    /**
     * 不追加文件，每次覆盖从头开始
     *
     * @param personLists
     * @param fileName
     */
    public static void doWrite(List<Person> personLists, String fileName) {
        try {
            aFile = new RandomAccessFile(fileName, "rw");
            inChannel = aFile.getChannel();
            buf.clear();
            for (Person p : personLists) {
                buf.put((p.toString() + "\n").getBytes());
            }
            buf.flip();
            while (buf.hasRemaining()) {
                inChannel.write(buf);
            }
            inChannel.close();
            System.out.println("Write Over");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 在文件末尾追加文件
     *
     * @param personLists
     * @param fileName
     */
    public static void doWriteAppend(List<Person> personLists, String fileName) {
        try {
            aFile = new RandomAccessFile(fileName, "rw");
            inChannel = aFile.getChannel();
            inChannel.position(inChannel.size());
            buf.clear();
            for (Person p : personLists) {
                buf.put((p.toString() + "\n").getBytes());
            }
            buf.flip();
            while (buf.hasRemaining()) {
                inChannel.write(buf);
            }
            inChannel.close();
            System.out.println("Write Over");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void doRead(String name) throws IOException {
        aFile = new RandomAccessFile(name, "rw");
        inChannel = aFile.getChannel();

        int bytesRead = inChannel.read(buf);
        while (bytesRead != -1) {
            System.out.println("Read " + bytesRead);
            buf.flip();
            while (buf.hasRemaining()) {
                System.out.print((char) buf.get());
            }
            buf.clear();
            bytesRead = inChannel.read(buf);
        }
        aFile.close();
    }

    public static void doCopy(String orignal, String destination) throws IOException {
        aFile = new RandomAccessFile(orignal, "rw");
        inChannel = aFile.getChannel();
        RandomAccessFile bFile = new RandomAccessFile(destination, "rw");
        FileChannel outChannel = bFile.getChannel();
        inChannel.transferTo(0, inChannel.size(), outChannel);
        System.out.println("Copy over");
    }

    /**
     * 获取当前系统的换行符
     * /r Mac
     * /n Unix/Linux
     * /r/n Windows
     * <p>
     * 注意在将流写入文件时，换行应根据操作系统的不同来决定。
     * 在程序我们应尽量使用System.getProperty("line.separator")来获取当前系统的换
     * 行符，而不是写/r/n或/n。
     * 这样写程序不够灵活
     * 当我们在java控制台输出的时候，/r和/n都能达到换行的效果。
     */
    public static void lineSeparator() {
        if (System.getProperty("line.separator").equals("\r\n")) {
            System.out.println("//r//n is for windows");
        } else if (System.getProperty("line.separator").equals("\r")) {
            System.out.println("//r is for Mac");
        } else if (System.getProperty("line.separator").equals("\n")) {
            System.out.println("//n is for Unix/Linux");
        } else {
            System.out.println("failed");
        }
    }

    public static void main(String[] args) throws IOException {
        NioFileUtils.lineSeparator();
    }

}
